/**
 * Copyright (c) 2019-2021,  陈俊兵 (cjb6b.com@gmail.com)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ktjiaoyu.thread.create.pool;

import java.util.concurrent.*;

/**
 *
 * @packageName com.ktjiaoyu.thread.create.pool
 * @fileName ThreadPoolTest.java
 * @author 陈俊兵 (cjb6b.com@gmail.com)
 * @createTime 2023/7/20 17:50
 * @lastModify 2023/7/20 17:50
 * @version 1.0.0
 */
public class ThreadPoolTest {

    public static void main(String[] args) {
        // 创建一个固定线程数量的线程池
        ExecutorService threadPool = new ThreadPoolExecutor(10,10, 0, TimeUnit.SECONDS,
                new LinkedBlockingDeque<>());
        // 创建缓存线程池
//       ExecutorService threadPool = Executors.newCachedThreadPool();
        // 创建单个线程
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();

//        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(10);

        for (int i = 0; i < 100; i++) {
            final int fi = i;
            threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() +
                            "正在打第" + (fi + 1) + "颗螺丝！");

            });
        }
        threadPool.shutdown();

        // 自旋等待线程池完成所有任务
        while(!threadPool.isTerminated()){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("所有人都打完螺丝了...");
    }
}
